import Vue from 'vue';
import Vuex from 'vuex';
import service from "@/api/service";
import appConfig from '@/config/appConfig.json';
Vue.use(Vuex)
let modules = {};
let storeConfig = appConfig.store;
for (let i = 0; i < storeConfig.modules.length; i++) {
  let moduleConfig = storeConfig.modules[i];
  let moduleObj = require('@/store/modules/' + moduleConfig.path)
  modules[moduleConfig.name] = moduleObj.default;
}
let getters = {};
for(let n=0; n< storeConfig.getters.length; n++){
  let getterConfig = storeConfig.getters[n];
  getters[getterConfig.name] = (state) => state[getterConfig.module][getterConfig.property];
}


const store = new Vuex.Store({
  modules,
  getters,
  state: {
    initialized: false,
    sessionInitialized: false,
    loading: false,
    token: null,
    defaultEnvConfig: {
      "logoUri": "/static/img/logo.png",
      "iconUri": "/static/img/logo.ico",
      "title": "系统管理平台",
      "description": "版权所有 武汉昇源科技",
      "copyRight": "Copyright © syo12.com All rights reserved.",
      "icp": "鄂ICP备2024036681号-1"
    },
    envConfig:{

    },
    identity: {}
  },
  mutations: {
    setInitialized(state, initialized) {
        state.initialized = initialized
    },
    setSessionInitialized(state, sessionInitialized) {
      state.sessionInitialized = sessionInitialized
    },
    setIdentity (state, identity) {
        state.identity = identity;
    },
    setToken (state, token) {
      state.token = token;
    },
    loading(state, loading) {
      state.loading = loading
    },
    setEnvConfig(state, config){
      state.envConfig = config;
      console.log(config)
    }
  },
  actions: {
    init({ dispatch, commit, state }) {

      return new Promise((resolve, reject) => {
        async function func(){
          let tasks = [];
          storeConfig.initializeItems.forEach(element => {

            tasks.push(dispatch(element));
          });
          let results = await Promise.all(tasks);

          commit('setInitialized', true);
          return results;
        };
        if(state.initialized){
          resolve();
        }
        else{
          func().then(resolve).catch(reject);
        }
      });
    },
    sessionInit({ dispatch, commit, state }) {

      return new Promise((resolve, reject) => {
        async function func(){
          let tasks = [];
          storeConfig.sessionInitialize.forEach(element => {

            tasks.push(dispatch(element));
          });
          let results = await Promise.all(tasks);

          commit('setSessionInitialized', true);
          return results;
        };
        if(state.sessionInitialized){
          resolve();
        }
        else{
          func().then(resolve).catch(reject);
        }
      });
    },
    getIdentity({commit, state}){
      return service.desktop
        .currentUser()
        .then((response) => {
          if (response.success) commit("setIdentity", response.data);
          return response;
        })
        .catch((error) => {
          return error;
        });
    },
    getToken({commit, state}){
      return service.oauthAgent
        .token()
        .then((response) => {
          if (response.success) commit("setToken", response.data);
          return response;
        })
        .catch((error) => {
          return error;
        });
    },
    getEnvConfig({commit, state}){
      return service.platform.envConfig((response)=>{

          if (response.success) commit("setEnvConfig", response.data);
          return response;
        });
    }
  }
});

export default store
